package com.naiterui.ehp.bs.emr.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecordTemplate;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.repository.mongo.MedicalRecordTemplateRepository;
import com.naiterui.ehp.bs.emr.service.IMedicalRecordTemplateService;
import com.naiterui.ehp.bs.emr.vo.MedicalRecordTemplateDetailVO;
import com.naiterui.ehp.bs.emr.vo.MedicalRecordTemplateVO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 病历模板服务实现类
 * 
 * @author yangfuya
 * @since 2022/4/11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MedicalRecordTemplateServiceImpl implements IMedicalRecordTemplateService {
    private final MedicalRecordTemplateRepository medicalRecordTemplateRepository;

    @Override
    public PageVO<MedicalRecordTemplateVO> findByDoctorId(Long doctorId, PageParamVO pageParamVO)
        throws BusinessException {
        Page<MedicalRecordTemplate> page =
            medicalRecordTemplateRepository.findByDoctorIdOrderByCreateAtDesc(doctorId, pageParamVO.getPageable());
        List<MedicalRecordTemplateVO> list = page.getContent().stream().map(
            s -> MedicalRecordTemplateVO.builder().id(s.getId()).doctorId(s.getDoctorId()).name(s.getName()).build())
            .collect(Collectors.toList());
        return PageVO.newBuilder().pageNo(pageParamVO.getPage()).pageSize(pageParamVO.getNum())
            .totalCount((int)page.getTotalElements()).result(list).build();
    }

    @Override
    public void save(Long doctorId, MedicalRecordTemplateDetailVO vo) throws BusinessException {
        String id = vo.getId();
        MedicalRecordTemplate medicalRecordTemplate =
            this.medicalRecordTemplateRepository.findByDoctorIdAndName(doctorId, vo.getName());
        if (medicalRecordTemplate != null && !medicalRecordTemplate.getId().equals(id)) {
            throw new BusinessException(ExceptionCodes.MEDICAL_RECORD_TEMPLATE_NAME_EXIST);
        }
        MedicalRecordTemplate template = BeanUtil.copyProperties(vo, MedicalRecordTemplate.class);
        template.setDoctorId(doctorId);
        template.setChangedAt(new Date());
        boolean updated = StrUtil.isNotEmpty(id) && StrUtil.isNotBlank(id);
        log.info("病历模板编辑：id={}, update={}", id, updated);
        if (updated) {
            MedicalRecordTemplate recordTemplate = this.medicalRecordTemplateRepository.get(id);
            if (recordTemplate == null) {
                throw new BusinessException(ExceptionCodes.PARAM_ERROR);
            }
            template.setCreateAt(recordTemplate.getCreateAt());
            this.medicalRecordTemplateRepository.update(template);
        } else {
            template.setCreateAt(new Date());
            this.medicalRecordTemplateRepository.save(template);
        }
    }

    @Override
    public MedicalRecordTemplateDetailVO findById(String id, Long doctorId) throws BusinessException {
        MedicalRecordTemplate medicalRecordTemplate =
            this.medicalRecordTemplateRepository.findByIdAndDoctorId(id, doctorId);
        if (medicalRecordTemplate == null) {
            throw new BusinessException(ExceptionCodes.MEDICAL_RECORD_TEMPLATE_NOT_EXIST);
        }
        MedicalRecordTemplateDetailVO medicalRecordTemplateDetailVO = new MedicalRecordTemplateDetailVO();
        BeanUtil.copyProperties(medicalRecordTemplate, medicalRecordTemplateDetailVO);
        return medicalRecordTemplateDetailVO;
    }
}
